#!/usr/bin/python
# -*- coding: utf-8 -*-

from tkinter import *
import requests
import re
from PIL import Image,ImageTk
import tkinter.messagebox as msgbox


def download():
	startUrl = 'http://www.uustv.com/'
	name = entry.get()
	if not name:
		msgbox.showinfo('提示', '请输入你的名字: ')
	else:
		data = {
			'word': name,
			'sizes':60,
			'fonts':'jfcs.ttf',
			'fontcolor':'#000000'
		}
		
		result = requests.post(startUrl, data = data)
		result.encoding = 'utf-8'
		
		req = '<div class="tu"><img src="(.*?)"/></div>'
		imgUrl = startUrl + (re.findall(req, result.text)[0])
		response = requests.get(imgUrl).content
		with open('{}.gif'.format(name), "wb") as f:
			f.write(response)
		im = Image.open('{}.gif'.format(name))

def helloCallBack():
	msgbox.showinfo( "Hello Python", "Hello World")
	# .showwarning
	# .showerror
	# .askquestion
	# .askokcancel
	# .askyesno

def center(win):
    """
    centers a tkinter window
    :param win: the root or Toplevel window to center
    """
    win.update_idletasks()
    width = win.winfo_width()
    frm_width = win.winfo_rootx() - win.winfo_x()
    win_width = width + 2 * frm_width
    height = win.winfo_height()
    titlebar_height = win.winfo_rooty() - win.winfo_y()
    win_height = height + titlebar_height + frm_width
    x = win.winfo_screenwidth() // 2 - win_width // 2
    y = win.winfo_screenheight() // 2 - win_height // 2
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    win.deiconify()
	
	
# Quick reference for tk GUI Component
# https://www.tutorialspoint.com/python/python_gui_programming.htm

root = Tk()
root.attributes('-alpha', 0.0)
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.title("看看你名字签名吧！")
root.geometry("500x500") #You want the size of the app to be 500x500
root.resizable(0, 0) #Don't allow resizing in the x or y direction

frm = Frame(root, bd=4, relief='raised')
frm.pack(fill='x')
labelframe = LabelFrame(frm, text="基本功能")
labelframe.pack(fill="both", expand="yes")

L1 = Label(labelframe, text="请输入你的名字:")
#L1.pack(side=LEFT)
L1.pack()

entry = Entry(labelframe, bd =5)
entry.pack()

B = Button(labelframe, text ="提交", command = helloCallBack)
#B.pack(side = RIGHT)
B.pack()

labelframe2 = LabelFrame(frm, text="设置")
labelframe2.pack(fill="both", expand="yes")
L2 = Label(labelframe2, text="设置内容:")
L2.pack()

labelframe3 = LabelFrame(frm, text="关于")
labelframe3.pack(fill="both", expand="yes")
L2 = Label(labelframe3, text="版本: v0.1.1")
L2.pack()


center(root)
root.attributes('-alpha', 1.0)
root.mainloop()

